﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using XmlDataType;

// -----------------------------------------------------------------------
// <copyright file="$safeitemrootname$.cs" company="$registeredorganization$">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace NewGame
{
    public class MapTile : VisibleGameEntity3D
    {
        private List<Vector2> _homeAPositions;
        private List<Vector2> _homeBPositions;
        private Vector4 rectRegion;
        private MapInfo _mapInfo;

        public MapInfo MapInfo
        {
            get { return _mapInfo; }
            set { _mapInfo = value; }
        }

        public Vector4 RectRegion
        {
            get { return rectRegion; }
            set { rectRegion = value; }
        }
        private Vector2 _size;

        public Vector2 Size
        {
            get { return _size; }
            set { _size = value; }
        }

        public List<Vector2> HomeAPositions
        {
            get { return _homeAPositions; }
            set { _homeAPositions = value; }
        }

        public List<Vector2> HomeBPositions
        {
            get { return _homeBPositions; }
            set { _homeBPositions = value; }
        }

        public MapTile(ContentManager content, string strHeighMap, int rowIndex, int colIndex, float scale, Vector3 pos, float rotationY, string filePathMapInfo)
        {
            HomeAPositions = new List<Vector2>();
            HomeBPositions = new List<Vector2>();
            _model = new MyTerrainModel(content, strHeighMap, rowIndex, colIndex, scale, pos, rotationY);
            Size = ((MyTerrainModel)_model).Size;

            _mapInfo = content.Load<MapInfo>(filePathMapInfo);
            setMapInfo(_mapInfo);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }



        public float getHeight(Vector3 pos)
        {
            return ((MyTerrainModel)_model).getHeight(pos);
        }

        public void setMapInfo(MapInfo _mapInfo)
        {
            HomeAPositions.Add(_mapInfo.ScaleMap * _mapInfo.HomeA1);
            HomeAPositions.Add(_mapInfo.ScaleMap * _mapInfo.HomeA2);
            HomeAPositions.Add(_mapInfo.ScaleMap * _mapInfo.HomeA3);
            HomeAPositions.Add(_mapInfo.ScaleMap * _mapInfo.HomeA4);
            HomeAPositions.Add(_mapInfo.ScaleMap * _mapInfo.HomeA5);

            HomeBPositions.Add(_mapInfo.ScaleMap * _mapInfo.HomeB1);
            HomeBPositions.Add(_mapInfo.ScaleMap * _mapInfo.HomeB2);
            HomeBPositions.Add(_mapInfo.ScaleMap * _mapInfo.HomeB3);
            HomeBPositions.Add(_mapInfo.ScaleMap * _mapInfo.HomeB4);
            HomeBPositions.Add(_mapInfo.ScaleMap * _mapInfo.HomeB5);

            rectRegion = _mapInfo.ScaleMap * new Vector4(_mapInfo.X, _mapInfo.Y, _mapInfo.Width, _mapInfo.Height);
        }

        public bool isValidPosition(Vector3 currentPosition)
        {
            if (currentPosition.X > rectRegion.X && currentPosition.X < rectRegion.X + rectRegion.Z
                && currentPosition.Z > rectRegion.Y && currentPosition.Z < rectRegion.Y + rectRegion.W)
                return true;
            return false;
        }
    }
}
